/* Response interceptor, exception handling =>zy */
import axios from 'axios'
import router from '@/router'
import { Notification, MessageBox,Message } from 'element-ui'
import store from '../store'
import { getToken } from '@/utils/auth'
import Config from '@/config'
const http = {}

var instance = axios.create({
    timeout: 5000,
    // baseURL
})


instance.interceptors.request.use(
    function(config) {
        if (getToken()) {
            // config.headers.Authorization = getToken()
            config.headers['token'] = getToken()
        }
        return config
    },
    function(error) {
        return Promise.reject(error)
    }
)


instance.interceptors.response.use(
    response => {
        // return response.data   //old，http状态码判断无须下面
        const res = response.data

        if (res.code === 2011 || res.code === 2007) {
           // to re-login
            MessageBox.confirm(
                '登录状态已过期，您可以继续留在该页面，或者重新登录',
                '系统提示',
                    {
                        confirmButtonText: '重新登录',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }
                ).then(() => {
                    store.dispatch('LogOut').then(() => {
                    location.reload() 
                })

            })
            return Promise.reject(res || 'error') 
        }else {
            return response.data
        }      
       

    },
    err => {
        if (err && err.response) {
            switch (err.response.status) {
            case 400:
                err.message = '请求出错'
                break
            case 401:
                Message.error({
                    message: '授权失败，请重新登录'
                })
                setTimeout(() => {
                    store.dispatch('LogOut').then(() => {
                      location.reload() 
                    })
                }, 1000)
                return
            case 403:
                Message.warning({
                    message: '你无权访问这个页面'
                })
                router.push({ path: '/401' })
                return
            case 404:
                err.message = '请求错误,未找到该资源'
                break
            case 500:
                err.message = '服务器端出错！'
                break
            case 504:
                err.message = '请求超时！请尝试刷新页面重试'
                break
            }

        } else {
            err.message = '连接服务器失败'
        }
        Message.error({
            message: err.message
        })
        return Promise.reject(err.response)
    }
)

http.get = function(url, options) {
    return new Promise((resolve, reject) => {
        instance
            .get(url, options)
            .then(response => {
                if (response.code ==="2000" || 2000) {
                    resolve(response)
                } else {
                    console.log("错误")
                    Message.error({
                        message: response.msg
                    })
                    reject(response.message)
                }
            })
            .catch(e => {
                console.log(e)
            })
    })
}

http.post = function(url, data, options) {
    return new Promise((resolve, reject) => {
        instance
            .post(url, data, options)
            .then(response => {
                if (response.code === 2000 || response.code === "2000") {     
                    resolve(response.data)
                } else {
                    Message.error({
                        message: response.msg
                    })
                    reject(response.message)
                }
            })
            .catch(e => {
                console.log(e)
            })
    })
}

export default http
